<template>
  <div>
    <el-row style="margin:5px 0">
      <el-col :span="24">
        <el-input
          placeholder="请输入物流公司"
          size="small"
          style="width:200px"
          v-model="obj.keyword"
          clearable
        >
        </el-input>
        <el-button
          size="small"
          @click="search"
          icon="el-icon-search"
          type="primary"
        ></el-button>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24" style="margin:5px 0">
        <el-button type="success" round @click="open" size="small"
          >添加</el-button
        >
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-table
          :data="tableData"
          style="width: 100%"
          height="500"
          v-loading="loading"
        >
          <el-table-column label="id" prop="id"> </el-table-column>
          <el-table-column label="姓名" width="180">
            <template slot-scope="scope">
              <div slot="reference" class="name-wrapper">
                <el-tag size="medium">{{ scope.row.name }}</el-tag>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="编码" prop="code"> </el-table-column>
          <el-table-column label="排序" prop="sort" sortable> </el-table-column>

          <el-table-column label="状态" prop="is_show">
            <template slot-scope="scope">
              <Sw
                :isShow="scope.row.is_show"
                :editId="scope.row.id"
                @switchChange="switchChange"
              ></Sw>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                @click="handleEdit(scope.$index, scope.row)"
                >编辑</el-button
              >
              <el-popconfirm
                confirm-button-text="确定"
                cancel-button-text="取消"
                icon="el-icon-info"
                icon-color="red"
                title="确定删除吗？"
                @confirm="handleDelete(scope.$index, { ...scope.row })"
                style="margin-left:5px"
              >
                <el-button slot="reference" type="danger" size="small"
                  >删除</el-button
                >
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <el-pagination
      background
      layout="sizes,prev, pager, next,jumper"
      :total="total"
      :page-sizes="[5, 8, 10]"
      :page-size="limit"
      :current-page="page"
      @current-change="pageChange"
      @size-change="sizeChange"
    >
    </el-pagination>
    <CompanyModel
      :title="title"
      :form="form"
      :ruleList="ruleList"
      @finish="finish"
    />
  </div>
</template>

<script>
import {
  getCompanyList,
  citySwicthChange,
  editCompanyList,
  conEdit,
  addModel,
  addQ,
  delCompany,
} from "../../api/Logistics";

import CompanyModel from "../../components/CompanyModel";
import Sw from "../../components/Sw";
export default {
  components: { CompanyModel, Sw },
  data() {
    return {
      loading: false,
      obj: {
        page: 1,
        limit: 10,
        keyword: "",
      },
      total: 0,
      ruleList: [],
      title: "",
      tableData: [],
      form: {
        id: "",
        name: "",
        sort: "",
        code: "",
        is_show: 0,
        dialogFormVisible: false,
      },
    };
  },

  methods: {
    search() {
      this.getData();
    },
    getData() {
      this.loading = true;
      const { page, limit, keyword } = this.obj;
      getCompanyList({ page, limit, keyword }).then((res) => {
        this.total = res.data.data.count;
        this.tableData = res.data.data.list;
        this.loading = false;
      });
    },
    handleEdit(index, row) {
      const { id, name, code, sort, is_show } = row;
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      editCompanyList(row.id).then((res) => {
        loading.close();
        this.title = res.data.data.title;
        this.form.dialogFormVisible = true;
        this.form.name = name;
        this.form.id = id;
        this.form.code = code;
        this.form.is_show = is_show;
        this.form.sort = sort;
        this.ruleList = res.data.data.rules;
      });
    },
    handleDelete(index, row) {
      delCompany(row.id).then((res) => {
        this.getData();
      });
    },
    pageChange(e) {
      this.loading = true;
      this.obj.page = e;
      this.getData();
    },
    sizeChange(e) {
      this.loading = true;
      this.obj.limit = e;
      this.getData();
    },
    switchChange(e) {
      this.loading = true;
      const { id, is_show } = e;
      citySwicthChange({ id, is_show }).then((res) => {
        this.getData();
      });
    },
    open() {
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      this.form = {
        id: "",
        name: "",
        sort: "",
        code: "",
        is_show: 0,
        dialogFormVisible: false,
      };

      addModel().then((res) => {
        loading.close();
        this.title = res.data.data.title;
        this.ruleList = res.data.data.rules;
        this.form.dialogFormVisible = true;
      });
    },
    finish(e) {
      this.loading = true;
      if (e.id) {
        conEdit(e).then((res) => {
          this.getData();
          this.form.dialogFormVisible = false;
        });
      } else {
        addQ(e).then((res) => {
          if (res.data.status == 200) {
            this.getData();
            this.form.dialogFormVisible = false;
          } else {
            this.$message({ type: "error", message: res.data.msg });
          }
        });
      }
    },
  },
  created() {
    this.getData();
  },
};
</script>
<style scope></style>
